package com.akk.platform.manage.business;

import com.akk.common.enums.AdminMenuEnums;
import com.akk.module.admin.entity.Admin;
import com.akk.module.admin.entity.AdminMenu;
import com.akk.module.admin.entity.AdminRoleMenu;
import com.akk.module.admin.service.AdminMenuService;
import com.akk.module.admin.service.AdminRoleMenuService;
import com.akk.utils.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.akk.utils.RestResponse.PLATFORM_ERROR_CODE;

@Service
public class AdminMenuBusiness {

    @Autowired
    AdminMenuService adminMenuService;
    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    public RestResponse menuList(Query query) {
        PageHelper.startPage(query.getPage(),query.getLimit());
        Map<String,Object>search = query.getSearch();
        List<AdminMenu> adminMenus = adminMenuService.findByParam(query.getSearch());
        return RestResponse.build(PageUtils.buildPage(adminMenus,(int) new PageInfo<>(adminMenus).getTotal(),query));
    }

    public RestResponse menuUpdate(AdminMenu adminMenu) {
        if(adminMenu.getMenuId().equals(Constants.PLATFORM_MANAGE_ROOT_MENU_ID)){
            return RestResponse.build(PLATFORM_ERROR_CODE,"顶级菜单不可修改");
        }
        AdminMenu adminMenu1 = adminMenuService.findByMenuId(adminMenu.getMenuId());
        if(adminMenu.getMenuType().intValue()!=adminMenu1.getMenuType().intValue()){
            return RestResponse.build(PLATFORM_ERROR_CODE,"菜单类型不可修改");
        }
        if(!adminMenu.getMenuUpId().equals(adminMenu1.getMenuUpId())){
            return RestResponse.build(PLATFORM_ERROR_CODE,"菜单上级不可修改");
        }
        adminMenu1.setMenuName(adminMenu.getMenuName());
        adminMenu1.setMenuUrl(adminMenu.getMenuUrl());
        adminMenu1.setMenuIcon(adminMenu.getMenuIcon());
        adminMenu1.setMenuPerms(adminMenu.getMenuPerms());
        adminMenuService.update(adminMenu1);
        return RestResponse.build("操作成功");
    }

    public RestResponse menuInfo(String menuId) {
        return RestResponse.build(adminMenuService.findByMenuId(menuId));
    }




    public RestResponse menuAdd(AdminMenu adminMenu) {

        if(adminMenu.getMenuUpId() == null)
            adminMenu.setMenuUpId(Constants.PLATFORM_MANAGE_ROOT_MENU_ID);
        AdminMenu menuUp = adminMenuService.findByMenuId(adminMenu.getMenuUpId());
        adminMenu.setMenuId(IDUtils.getPK());
        if(menuUp.getMenuType().intValue() == AdminMenuEnums.TypeEnum.CATALOG.getValue()){
            if(adminMenu.getMenuType().intValue() == AdminMenuEnums.TypeEnum.CATALOG.getValue()){
                adminMenu.setMenuUrl(null);
            }
            if(adminMenu.getMenuType().intValue() == AdminMenuEnums.TypeEnum.MENU.getValue()&& adminMenu.getMenuUrl()==null)
                return RestResponse.build(PLATFORM_ERROR_CODE,"菜单的URL不能为空");
            if(adminMenu.getMenuType().intValue() == AdminMenuEnums.TypeEnum.BUTTON.getValue())
                return RestResponse.build(PLATFORM_ERROR_CODE,"目录下只能创建菜单");
        }else if(menuUp.getMenuType().intValue() == AdminMenuEnums.TypeEnum.MENU.getValue()){
            if(adminMenu.getMenuType().intValue() == AdminMenuEnums.TypeEnum.BUTTON.getValue()){
                adminMenu.setMenuUrl(null);
            }else{
                return RestResponse.build(PLATFORM_ERROR_CODE,"菜单下只能创建按钮");
            }
        }else{
            return RestResponse.build(PLATFORM_ERROR_CODE,"上级菜单类型错误.");
        }
        adminMenu.setMenuUpName(menuUp.getMenuName());
        adminMenuService.save(adminMenu);
        return RestResponse.build("操作成功");
    }


    @Transactional
    public RestResponse menuDel(String menuId) {
        if(menuId.equals(Constants.PLATFORM_MANAGE_ROOT_MENU_ID))
            return RestResponse.build(PLATFORM_ERROR_CODE,"顶级菜单不允许删除");
        if(!adminMenuService.findByMenuUpId(menuId).isEmpty()){
            return RestResponse.build(PLATFORM_ERROR_CODE,"存在下级菜单,删除失败");
        }
        adminRoleMenuService.deleteBatchByMenuId(menuId);
        adminMenuService.delete(menuId);
        return RestResponse.build("菜单删除成功");
    }

    public RestResponse allMenuTree() {
        List<AdminMenu> allMenu = adminMenuService.findAllMenu();
        //组装菜单
        List<AdminMenu> menuTree = adminMenuService.getMenuTree(allMenu);
        return RestResponse.build(menuTree);
    }

    public RestResponse roleMenuTree(String roleId) {
        List<AdminRoleMenu> roleMenus = adminRoleMenuService.findMenuByRoleId(roleId);
        List<String> menuIds = new ArrayList<>();
        for(AdminRoleMenu roleMenu:roleMenus){
            menuIds.add(roleMenu.getMenuId());
        }
        List<AdminMenu> menus = adminMenuService.findByMenuIds(menuIds);
        List<AdminMenu>menuTree = adminMenuService.getMenuTree(menus);
        return RestResponse.build(menuTree);
    }
}
